package hc.appt.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import hc.appt.Diagnosis;
import hc.service.AService;

public class DiagnosisService extends AService {

	public boolean doSave(int diaId, int apptId, String comments) {

		String sql = "insert into Diagnosis values(" + diaId + ", " + apptId
				+ ", '" + comments + "'";

		boolean re = this.insert(sql);
		return re;
	}

	public ArrayList<Diagnosis> getHistoryRecords(String pId) {

		String sql = "select * from Diagnosis d , scheduledetail s"
				+ " where d.apptid=s.scheduleid " + "and s.patientid='" + pId
				+ "'";
		Statement sm = null;
		ResultSet rs = null;

		ArrayList<Diagnosis> alist = new ArrayList<Diagnosis>();

		try {
			sm = connection.createStatement();
			rs = sm.executeQuery(sql);

			while (rs != null && rs.next()) {
				Diagnosis e = new Diagnosis();
				e.setApptId(rs.getInt("apptid"));
				e.setDiagnosisId(rs.getInt("diagnosisID"));
				e.setComment(rs.getString("comments"));
				alist.add(e);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (sm != null) {
					sm.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return alist;
	}

	public Diagnosis getDiagnosis(int diaId) {
		Statement sm = null;
		ResultSet rs = null;
		String sql = "select * from Diagnosis d where d.diagnosisid=" + diaId;
		Diagnosis diae = null;
		try {
			sm = connection.createStatement();
			rs = sm.executeQuery(sql);
			if (rs != null && rs.next()) {
				diae = new Diagnosis();
				diae.setApptId(rs.getInt("apptid"));
				diae.setDiagnosisId(rs.getInt("diagnosisid"));
				diae.setComment(rs.getString("commnets"));
			}
			rs.close();
			sm.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (sm != null) {
					sm.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return diae;
	}

}
